package org.crackjack.android.activity;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.crackjack.android.R;
import org.crackjack.android.util.HttpClient;
import org.crackjack.android.util.ResponseJsonParser;
import org.crackjack.android.util.UrlBuilder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AuthorizationActivity extends GenericActivity implements OnClickListener {

    public static final String AUTHORIZATION_STATUS = "authorization_status";

    private Button registrationButton;
    private Button authorizationButton;

    private EditText loginEditText;
    private EditText passwordEditText;

    private String json = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authorization);

        authorizationButton = (Button) findViewById(R.id.button_authorization);
        registrationButton = (Button) findViewById(R.id.button_registration);

        loginEditText = (EditText) findViewById(R.id.edit_text_login);
        passwordEditText = (EditText) findViewById(R.id.edit_text_password);

        authorizationButton.setOnClickListener(this);
        registrationButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_authorization:
                HttpClient client = new HttpClient();

                try {
                    json = client.makeGetRequest(UrlBuilder.buildAuthorizationUrl(loginEditText.getEditableText().toString(),
                            passwordEditText.getEditableText().toString()));
                    ResponseJsonParser.parseJson(json);
                    Intent intent = new Intent();
                    intent.setClass(this, MainActivity.class);
                    intent.putExtra(AuthorizationActivity.AUTHORIZATION_STATUS, json);
                    startActivity(intent);

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.button_registration:
                startActivity(RegistrationActivity.class);
                break;
            default:
                break;
        }

    }

}
